<?php  
class ControllerPageSitemap extends FrontController {
	public function index() {
		$this->document->setTitle($this->_['heading_title']); 

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['heading_title'],
			'href'      => U('page/sitemap'),      	
        	'separator' => $this->_['text_separator']
      	);
		
		M('catalog/category');
		M('catalog/product');
		
		$this->data['categories'] = array();
					
		$categories_1 = $this->model_catalog_category->getCategories(0);
		
		foreach ($categories_1 as $category_1) {
			$level_2_data = array();
			
			$categories_2 = $this->model_catalog_category->getCategories($category_1['category_id']);
			
			foreach ($categories_2 as $category_2) {
				$level_3_data = array();
				
				$categories_3 = $this->model_catalog_category->getCategories($category_2['category_id']);
				
				foreach ($categories_3 as $category_3) {
					$level_3_data[] = array(
						'name' => $category_3['name'],
						'href' => $category_3['external_link'] ? $category_3['external_link'] : U('product/category', 'category_id=' . $category_3['category_id'])
					);
				}
				
				$level_2_data[] = array(
					'name'     => $category_2['name'],
					'children' => $level_3_data,
					'href'     => $category_2['external_link'] ? $category_2['external_link'] : U('product/category', 'category_id=' . $category_2['category_id'])	
				);					
			}
			
			$this->data['categories'][] = array(
				'name'     => $category_1['name'],
				'children' => $level_2_data,
				'href'     => $category_1['external_link'] ? $category_1['external_link'] : U('product/category', 'category_id=' . $category_1['category_id'])
			);
		}
		
		M('catalog/page');		
		$this->data['pages'] = array();
    	
		foreach ($this->model_catalog_page->getpages() as $result) {
      		$this->data['pages'][] = array(
        		'title' => $result['title'],
        		'href'  => U('page/index', 'page_id=' . $result['page_id']), 
      		);
    	}
		
		$this->display('page/sitemap.tpl');
	}
}
?>